﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormTest
{
    public partial class UcListView:ListView
    {
        private ImageList _imgList; //图标列表
        #region
        //private const int LVM_SETHEADERHEIGHT = 0x1000 + 31;
        //[DllImport("user32.dll", CharSet = CharSet.Auto)]
        //private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        //// 自定义标题高度属性
        //private int _headerHeight = 40;
        //public int HeaderHeight
        //{
        //    get => _headerHeight;
        //    set
        //    {
        //        _headerHeight = value;
        //        // 发送消息设置标题高度
        //        SendMessage(Handle, LVM_SETHEADERHEIGHT, (IntPtr)150, IntPtr.Zero);
        //        Invalidate();
        //    }
        //}
        #endregion

        public UcListView() {

            _imgList = new ImageList();
            // 启用所有者绘制模式
            OwnerDraw = true;
            View = View.Details;
            GridLines = true;
            AllowColumnReorder = true;
            FullRowSelect = true;
            SmallImageList = _imgList;
            SmallImageList.ImageSize = new Size(32, 32);
            CheckBoxes = true;

            InitializeEditTextBox();

            // 订阅绘制事件
            DrawColumnHeader += OnDrawColumnHeader;
            DrawItem += OnDrawItem;
            DrawSubItem += OnDrawSubItem;
            Resize += UcListView_Resize;
            ColumnClick+= OnColumnClick;
            MouseClick += OnMouseClick;
        }
        #region 事件处理
        private void UcListView_Resize(object sender, EventArgs e)
        {
            int totalWidth = this.ClientRectangle.Width - 60 - 60-60;
            if (Columns.Count > 0)
            {
                Columns[0].Width = totalWidth * 2 / 10;
                Columns[1].Width = totalWidth * 3 / 10;
                Columns[2].Width = totalWidth * 5 / 10;
                Columns[3].Width = 60;
                Columns[4].Width = 60;
                Columns[5].Width = 60;
            }
        }
        private void OnDrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            // 设置列标题高度
            Rectangle headerRect = e.Bounds;
            //// 2. 绘制列头背景（渐变效果）
            using (LinearGradientBrush brush = new LinearGradientBrush(headerRect,Color.FromArgb(230, 230, 250),Color.FromArgb(190, 190, 230),LinearGradientMode.Vertical))
            {
                e.Graphics.FillRectangle(brush, headerRect);
            }

            //// 3. 绘制列头文本（垂直居中）
            if (e.ColumnIndex < 4)
            {
                StringFormat format = new StringFormat
                {
                    Alignment = StringAlignment.Center, // 水平居中
                    LineAlignment = StringAlignment.Center // 垂直居中
                };
                e.Graphics.DrawString(e.Header.Text, new Font("微软雅黑", 11, FontStyle.Bold), Brushes.Green, headerRect, format);
            }
            else if(e.ColumnIndex==4) //操  水平向右
            {
                StringFormat format = new StringFormat
                {
                    Alignment = StringAlignment.Far, // 水平向右
                    LineAlignment = StringAlignment.Center // 垂直居中
                };
                e.Graphics.DrawString(e.Header.Text, new Font("微软雅黑", 11, FontStyle.Bold), Brushes.Green, headerRect, format);
            }
            else  //作  水平向左
            {
                StringFormat format = new StringFormat
                {
                    Alignment = StringAlignment.Near, // 水平向左
                    LineAlignment = StringAlignment.Center // 垂直居中
                };
                e.Graphics.DrawString(e.Header.Text, new Font("微软雅黑", 11, FontStyle.Bold), Brushes.Green, headerRect, format);
            }
            //// 绘制边框
            if (e.ColumnIndex < 5)
            {
                e.Graphics.DrawRectangle(Pens.LightGray, headerRect);
            }
        }
        private void OnDrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawDefault = true;
        }

        private void OnDrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            e.DrawDefault = true;
        }

        
        #endregion
        public void AddDefaultColumns()
        {
            int totalWidth = this.ClientRectangle.Width - 60 -60-60;
            Columns.Add("原文件名", totalWidth*2/10);
            Columns.Add("新文件名",totalWidth *3/10);
            Columns.Add("文件路径", totalWidth *5/10);
            Columns.Add("状态", 60);
            Columns.Add("操", 60);
            Columns.Add("作", 60);
        }

        public void AddListViewItem(string fileName,string newName="")
        {
            Icon icon = Icon.ExtractAssociatedIcon(fileName); // 获取文件图标
            _imgList.Images.Add(icon);  // 添加到图标列表中 
            int imgIdx = _imgList.Images.Count - 1; // 获取图标索引
            ListViewItem item = new ListViewItem(Path.GetFileName(fileName)); // 创建列表项，显示文件名
            if (string.IsNullOrEmpty(newName)==true)
            {
                item.SubItems.Add(Path.GetFileName(fileName));
            }
            else
            {
                item.SubItems.Add(newName);
            }
            item.SubItems.Add(Path.GetDirectoryName(fileName));
            item.SubItems.Add("待操作");
            item.SubItems.Add("删除");
            item.SubItems.Add("打开");
            item.ImageIndex = imgIdx;
            Items.Add(item);
        }
    }
}
